package cn.bb.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.bb.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.bb.common.utils.R;
import cn.bb.gulimall.product.entity.CategoryEntity;
import cn.bb.gulimall.product.service.CategoryService;


/**
 * 商品三级分类
 *
 * @author sharpbb
 * @email 1029922944@gmail.com
 * @date 2020-11-25 15:01:21
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 展示出父子分级树形结构
     */
    @RequestMapping("/list/tree")
    public R list(){
       List<CategoryEntity> categoryEntities= categoryService.listWithTree();
        return R.ok().put("data", categoryEntities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     * 注意 @RequestBody的都是带请求体的 需要用post
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        categoryService.removeLogicByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
